package cn.gitbook.securityshare.util;

import cn.gitbook.securityshare.common.CodeMsg;
import cn.gitbook.securityshare.common.JsonObject;
import cn.gitbook.securityshare.exception.IllegalTokenException;
import cn.gitbook.securityshare.exception.MethodNotSupportException;
import cn.gitbook.securityshare.exception.UkeyNotSupportException;
import cn.gitbook.securityshare.exception.UserForceLoginOutException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HandleHttpErrorUtil {
    private HandleHttpErrorUtil() {
    }

    public static void handleHttpError(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws Exception {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);//设置http 返回请求错误码
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        JsonObject<String> resultVo = new JsonObject<>();
        resultVo.setData("");
        if (IllegalTokenException.class.isAssignableFrom(e.getClass())) {
            resultVo.setCode(CodeMsg.NO_AUTH_ERROR.getCode());
            resultVo.setMessage(CodeMsg.NO_AUTH_ERROR.getMessage());
            //如果有单点登录的处理，需要在此处定制返回的错误信息，单点登录页属于认证出现了业务异常
        } else if (BadCredentialsException.class.isAssignableFrom(e.getClass())) {
            resultVo.setCode(CodeMsg.passwordIncorrect.getCode());
            resultVo.setMessage(CodeMsg.passwordIncorrect.getMessage());
        } else if (UserForceLoginOutException.class.isAssignableFrom(e.getClass())) {
            resultVo.setCode(CodeMsg.forceLoginOut.getCode());
            resultVo.setMessage(CodeMsg.forceLoginOut.getMessage());
        } else if (MethodNotSupportException.class.isAssignableFrom(e.getClass())) {
            resultVo.setCode(CodeMsg.methodNotSupport.getCode());
            resultVo.setMessage(CodeMsg.methodNotSupport.getMessage());
        } else if (UkeyNotSupportException.class.isAssignableFrom(e.getClass())) {
            resultVo.setCode(CodeMsg.ukeyNotSupported.getCode());
            resultVo.setMessage(CodeMsg.ukeyNotSupported.getMessage());
        } else {
            resultVo.setCode(CodeMsg.NO_AUTH_ERROR.getCode());
            resultVo.setMessage(CodeMsg.NO_AUTH_ERROR.getMessage());
        }
        response.getOutputStream().write(JSON.toJSONBytes(resultVo, SerializerFeature.BrowserCompatible));
    }
}
